package com.atguigu.gmall.cart.controller;

import com.atguigu.gmall.cart.service.CartService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.model.cart.CartInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author mgx
 * @version 1.0
 * @description: TODO
 * @date 2022-12-03 1:04
 */
@RestController
@RequestMapping("/api/cart")
@SuppressWarnings("all")
public class CartApiController {

    @Autowired
    private CartService cartService;


    /**
     * /api/cart/addToCart/skuId/skuNum
     * 加入购物车
     * @param skuId
     * @param skuNum
     * @param request
     * @return
     */
    @GetMapping("/addToCart/{skuId}/{skuNum}")
    public Result addToCart(@PathVariable Long skuId,
                            @PathVariable Integer skuNum, HttpServletRequest request) {
        // 获取用户id或者临时id
        String userId = AuthContextHolder.getUserId(request);
        if(StringUtils.isEmpty(userId)) {
            userId = AuthContextHolder.getUserTempId(request);
        }
        cartService.addToCart(userId, skuId, skuNum);
        return Result.ok();
    }


    /**
     * 展示购物车
     * /api/cart/cartList
     * @return
     */
    @GetMapping("/cartList")
    public Result cartList(HttpServletRequest request) {
        // 考虑到要合并购物车，所以用户id和临时id都需要获取到
        String userId = AuthContextHolder.getUserId(request);
        String userTempId = AuthContextHolder.getUserTempId(request);
        List<CartInfo> cartInfoList = cartService.getCartList(userId, userTempId);
        return Result.ok(cartInfoList);
    }


    /**
     * 购物车选中状态的变更
     * /api/cart/checkCart/{skuId}/{isChecked}
     * @param skuId
     * @param isChecked
     * @return
     */
    @GetMapping("/checkCart/{skuId}/{isChecked}")
    public Result checkCart(@PathVariable Long skuId,
                            @PathVariable Integer isChecked, HttpServletRequest request) {
        // 获取用户id
        String userId = AuthContextHolder.getUserId(request);
        if(StringUtils.isEmpty(userId) ) {
            userId = AuthContextHolder.getUserTempId(request);
        }
        cartService.checkCart(userId, skuId, isChecked);
        return Result.ok();
    }


    /**
     * 删除购物车
     * /api/cart/deleteCart/skuId
     * @param skuId
     * @param request
     * @return
     */
    @DeleteMapping("/deleteCart/{skuId}")
    public Result deleteCart(@PathVariable Long skuId, HttpServletRequest request) {
        // 获取用户id
        String userId = AuthContextHolder.getUserId(request);
        if(StringUtils.isEmpty(userId) ) {
            userId = AuthContextHolder.getUserTempId(request);
        }
        cartService.deleteCart(userId, skuId);
        return Result.ok();
    }


    /**
     * 查询用户选中状态的购物车信息
     * /api/cart/getCartCheckedList/{userId}
     * @param userId
     * @return
     */
    @GetMapping("/getCartCheckedList/{userId}")
    public List<CartInfo> getCartCheckedList(@PathVariable Long userId) {
        return cartService.getCartCheckedList(userId);
    }
}
